<?php

namespace app\api\service;

use app\api\model\NoticeRead;
use app\api\model\UserNotice;

class NoticeService
{
    public static function getNoticeList($userInfo, $params): array
    {
        $list = UserNotice::getNoticeListByUserId($userInfo['id'], $params['page_no'], $params['page_size']);
        $data = array_map(function ($item) use ($userInfo) {
            return [
                'notice_id' => $item['id'],
                'notice_title' => $item['title'],
                'notice_describe' => $item['describe'],
                'notice_content' => $item['content'],
                'notice_time' => date('m月d日', strtotime($item['create_time'])),
                'notice_read' => (bool)NoticeRead::checkUserRead($item['id'], $userInfo['id']),
            ];
        }, $list);
        return [200, '操作成功', $data];
    }

    public static function userReadNotice($userInfo, $params): array
    {
        $result = NoticeRead::checkUserRead($params['notice_id'], $userInfo['id']);
        if (!$result) $result = NoticeRead::recordUserRead($params['notice_id'], $userInfo['id']);
        return [$result ? 200 : 201, $result ? '操作成功' : '操作失败', []];
    }
}